package com.ruzin.diplomska;

import java.util.ArrayList;

import com.ruzin.diplomska.Termin;

public class XmlParsing {

	public static String XMLKorisnik(String xml) {
		String XmlKorisnik = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
				+ "\n";
		int i = 0;
		boolean copy = false;
		for (i = 0; i < xml.length(); i++) {
			if (xml.charAt(i) == '<' && xml.charAt(i + 1) == 'k'
					&& xml.charAt(i + 2) == 'o') {
				copy = true;
			}
			if (copy) {
				XmlKorisnik += xml.charAt(i);
			}
			if (xml.charAt(i) == '>' && xml.charAt(i - 8) == 'k'
					&& xml.charAt(i - 9) == '/') {
				copy = false;
			}
		}
		return XmlKorisnik;
	}

	public static Pacient xmlGetPacient(String pac) {
		pac = pac.trim();
		pac = pac.replace(" ", "");
		String info[] = pac.split("\n");
		String id = info[2].replace("<id>", "").replace("</id>", "");
		String ime = info[3].replace("<ime>", "").replace("</ime>", "");
		String prezime = info[4].replace("<prezime>", "").replace("</prezime>",
				"");
		String EMBG = info[5].replace("<embg>", "").replace("</embg>", "");
		Pacient pacient = new Pacient(id, ime, prezime, EMBG);
		return pacient;
	}

	public static ArrayList<Termin> xmlGetTermini(String xml) {
		ArrayList<Termin> lista = new ArrayList<Termin>();
		xml = xml.trim();
		xml = xml.replace(" ", "");

		System.out.println(xml);
		String info[] = xml.split("\n");
		for (int i = 10; i < info.length; i += 7) {
			Termin t = new Termin();
			if (info[i].equals("</podatoci>")) {
				break;
			} else {
				
				String id = info[i].replace("<idter>", "").replace("</idter>",
						"");
				System.out.println("id: " + id);
				t.setIdter(id);
			}
			String datum = info[i + 1].replace("<datum>", "").replace(
					"</datum>", "");
			System.out.println("datum: " + datum);
			String vreme = info[i + 2].replace("<vreme>", "").replace(
					"</vreme>", "");
			System.out.println("vreme: " + vreme);
			String tretman = info[i + 3].replace("<tretman>", "").replace(
					"</tretman>", "");
			System.out.println("tretman: " + tretman);
			t.setDatum(datum);
			t.setVreme(vreme);
			t.setTretman(tretman);
			lista.add(t);
		}
		return lista;
	}

	public static String[] listToArray(ArrayList<Termin> lista) {
		String array[] = new String[lista.size()];
		for (int i = 0; i < lista.size(); i++) {
			Termin t = new Termin();
			t = lista.get(i);
			String niza = t.getDatum() + " " + t.getVreme();
			System.out.println(niza);
			array[i] = niza;
		}
		return array;
	}
}
